home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04083a < prev    next >
Text File  |  1991-02-17  |  2KB  |  74 lines

  1. /********************************************************************/
  2. /*            Byte classes.    Copyright by Joe Schell 1989.             */
  3. /********************************************************************/
  4.  
  5. #ifndef CLASS_byte
  6. #define CLASS_byte
  7.  
  8. #include <limits.h>        // Maximum values UCHAR_MAX and UINT_MAX.
  9. #include <stdlib.h>        // prototype exit() and EXIT_FAILURE.
  10. #include <iostream.h>
  11. #include <form.h>
  12.  
  13. /*------------------------------------------------------------------*/
  14. /* byte                Handle a byte.                                 */
  15. /*------------------------------------------------------------------*/
  16. class byte
  17.     {
  18. public:
  19.     byte()            { c = 0; }
  20.     byte(int &i)    { c = value(i); }
  21.     operator int() const { return c; }
  22.     byte operator=(int &i)    { c = value(i); return *this; }
  23.     byte operator++()    { c++; return *this; }
  24.     byte operator--()    { c--; return *this; }
  25.     char *make_string()    { return form("%2.2X", int(c));}
  26.  
  27. private:
  28.     unsigned char c;            // A byte.
  29.     unsigned char value(int &i)
  30.         {
  31.         if (i > UCHAR_MAX)
  32.             {
  33.             cerr << "\nByte class: Illegal value-" << i << "\n";
  34.             exit(EXIT_FAILURE);
  35.             }
  36.         return (unsigned char)i;
  37.         }
  38.     };    // End of byte class.
  39.  
  40. /*------------------------------------------------------------------*/
  41. /* word            Handle a word                                       */
  42. /*------------------------------------------------------------------*/
  43. class word
  44.     {
  45. public:
  46.     word()            { i = 0; }
  47.     word(long &x)    { i = value(x); }
  48.     operator long() const { return (long)i; }
  49.     word operator++()    { i++; return *this; }
  50.     word operator--()    { i--; return *this; }
  51.     char *make_string()    { return form("%4.4X", i); }
  52. private:
  53.     unsigned int i;        // An int.
  54.     unsigned int value(long &x)
  55.         {
  56.         if (x > UINT_MAX)
  57.             {
  58.             cerr << "\nWord class: Illegal value-" << x << "\n";
  59.             exit(EXIT_FAILURE);
  60.             }
  61.         return (unsigned int)x;
  62.         }
  63.     };    // End of word class.
  64.  
  65.  
  66. #if sizeof(unsigned char) != sizeof(byte)
  67.     #error  Byte class cannot be used as pointer to memory.
  68. #endif
  69. #if sizeof(unsigned int) != sizeof(word)
  70.     #error  Word class cannot be used as pointer to memory.
  71. #endif
  72.  
  73. #endif        // #ifndef CLASS_byte
  74.